const { Fans } = require('../../models/Fans')

// 关注作者
const follow = async (ctx, next) => {
  let fans = ctx.request.body
  let res = await Fans.create(fans)
  if(res){
    ctx.body = {
      code: 200,
      msg: "关注成功"
    }
  }else{
    ctx.body = {
      code: 300,
      msg: "关注失败"
    }
  }
}
// 取消关注作者
const unFollow = async (ctx, next) => {
  let fans = ctx.request.body
  let res = await Fans.deleteOne(fans)
  if(res){
    ctx.body = {
      code: 200,
      msg: "取关成功"
    }
  }else{
    ctx.body = {
      code: 300,
      msg: "取关失败"
    }
  }
}

// 查询粉丝数量
const totalFans = async (ctx, next) => {
  let { author } = ctx.request.body
  let res = await Fans.find({ author })
  console.log(res);
  if(res.length){
    ctx.body = {
      code: 200,
      data: res
    }
  }else{
    ctx.body = {
      code: 300,
      msg: "粉丝列表获取失败"
    }
  }
}


module.exports = {
  follow,
  unFollow,
  totalFans
}